<?php

namespace app\common\dao\user;


use app\common\dao\BaseDao;
use app\common\model\BaseModel;
use app\common\model\user\UserIntegralConfig;

class UserIntegralConfigDao extends BaseDao
{

    /**
     * @return string
     * @author: fupeng
     * @day: 2024/3/21
     */
    protected function getModel(): string
    {
        return UserIntegralConfig::class;
    }

    /**
     * @param array $where
     * @param $data
     * @return int
     * @throws \think\db\exception\DbException
     * @author: fupeng
     * @day: 2024/3/21
     */
    public function updateBill(array $where, $data)
    {
        return UserIntegralConfig::getDB()->where($where)->limit(1)->update($data);
    }


    /**
     * @param array $where
     * @return \think\db\BaseQuery
     * @author: fupeng
     * @day: 2024/3/21
     */
    public function search(array $where)
    {
        return UserIntegralConfig::getDB()
            ->when(isset($where['now_money']) && in_array($where['now_money'], [0, 1, 2]), function ($query) use ($where) {
                if ($where['now_money'] == 0)
                    $query->where('category', 'now_money')->whereIn('type', ['pay_product', 'recharge', 'sys_inc_money', 'sys_dec_money', 'brokerage', 'presell', 'refund']);
                else if ($where['now_money'] == 1)
                    $query->where('category', 'now_money')->whereIn('type', ['pay_product', 'sys_dec_money', 'presell']);
                else if ($where['now_money'] == 2)
                    $query->where('category', 'now_money')->whereIn('type', ['recharge', 'sys_inc_money', 'brokerage', 'refund']);
            })
            ->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
                $query->where('uid', $where['uid'])->where('mer_id', 0);
            })
            ->when(isset($where['pm']) && $where['pm'] !== '', function ($query) use ($where) {
                $query->where('pm', $where['pm']);
            })
            ->when(isset($where['category']) && $where['category'] !== '', function ($query) use ($where) {
                $query->where('category', $where['category']);
            })
            ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
                $query->where('status', $where['status']);
            })
            ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
                getModelTime($query, $where['date'], 'create_time');
            })
            ->when(isset($where['day']) && $where['day'] !== '', function ($query) use ($where) {
                $query->whereDay('create_time', $where['day']);
            })
            ->when(isset($where['month']) && $where['month'] !== '', function ($query) use ($where) {
                $query->whereMonth('create_time', $where['month']);
            })
            ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
                $data = explode('/', $where['type'], 2);
                if (count($data) > 1) {
                    $query->where('category', $data[0])->where('type', $data[1]);
                } else {
                    $query->where('type', $where['type']);
                }
            })
            ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
                $query->where('mer_id', $where['mer_id']);
            })
            ->when(isset($where['link_id']) && $where['link_id'] !== '', function ($query) use ($where) {
                $query->where('link_id', $where['link_id']);
            });
    }

    public function userNowMoneyIncTotal($uid)
    {
        return $this->search(['uid' => $uid, 'now_money' => 2])->sum('number');
    }

}
